package com.ymatou.shop.ui.activity;

import com.ymatou.shop.R;
import com.ymatou.shop.api.ApiRequest;
import com.ymatou.shop.api.ApiResponse;
import com.ymatou.shop.cache.RequestInfo;
import com.ymatou.shop.cache.exception.CacheException;
import com.ymatou.shop.model.LoginResult;
import com.ymatou.shop.model.ResponseMessage;
import com.ymatou.shop.util.DialogUtil;
import com.ymatou.shop.util.GlobalUtil;
import com.ymatou.shop.util.LogUtil;
import com.ymatou.shop.util.StringUtil;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.SlidingDrawer;

public class LoginActivity extends BaseHeaderActivity implements OnClickListener{

	private static final int REQUEST_ID_LOGIN = 0x01;
	private static final int REQUEST_ID_USERINFO = 0x02;
	private static final int REQUEST_ID_PERMISSION = 0x03;

	private EditText usernameEditText, passwordEditText;
	private Button loginButton, registerButton;
	private SlidingDrawer mSlidingDrawer;
	private ImageButton mImageButton;
	private LinearLayout mLinearLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_activity);

		setupViews();

		drawerlistener();

		//		GlobalUtil.startActivity(this, MainActivity.class);
		//		finish();
	}

	@Override
	protected void setupViews() {
		super.setupViews();

		
		getTitleLeftImageButton().setImageResource(R.drawable.back_icon);
		getTitleLeftImageButton().setOnClickListener(this);
		getTitleLeftImageButton().setVisibility(View.VISIBLE);
		getTitleLeftImageButton().setBackgroundDrawable(null);

		usernameEditText = (EditText) findViewById(R.id.username);
		passwordEditText = (EditText) findViewById(R.id.password);
		usernameEditText.setText("pengzhibang");
		passwordEditText.setText("123456");

		//	卖家测试账户wesper
		//	买家测试账户pengzhibang
		
		loginButton = (Button) findViewById(R.id.login);
		loginButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				String username = usernameEditText.getText().toString();
				String password = passwordEditText.getText().toString();
				if (StringUtil.isEmpty(username)){
					usernameEditText.requestFocus();
					return;
				}
				if (StringUtil.isEmpty(password)){
					passwordEditText.requestFocus();
					return;
				}

				//				getUserinfo("beb9056878ed4796bdccd6596b5930e2", "4085");
				showProgressDialog();
				RequestInfo request = ApiRequest.getLoginRequest(username, password);
				getCacheManager().register(REQUEST_ID_LOGIN, request, LoginActivity.this);
			}
		});
		registerButton = (Button) findViewById(R.id.register);

		mSlidingDrawer = (SlidingDrawer) findViewById(R.id.slidingdrawer);
		mLinearLayout = (LinearLayout) findViewById(R.id.handle);
		//		mImageButton = (ImageButton)findViewById(R.id.handle);
	}

	private void getUserinfo() {
		//验证用户身份
		RequestInfo request = ApiRequest.getUserinfo();
		getCacheManager().register(REQUEST_ID_USERINFO, request, LoginActivity.this);
	}
	
	private void getPermission() {
		RequestInfo request = ApiRequest.permission();
		getCacheManager().register(REQUEST_ID_PERMISSION, request, LoginActivity.this);
	}
	
	@Override
	public void onCacheSuccess(int requestId, RequestInfo requestInfo, Object response) {
		LogUtil.systemErr("" + response);
		switch (requestId) {
		case REQUEST_ID_LOGIN:
			LoginResult result = ApiResponse.getLoginResult((String) response);
			if (result != null) {
				getAccount().login(result.mUserId, result.mToken);

				getUserinfo();
			}
			break;
		case REQUEST_ID_USERINFO:
			result = ApiResponse.getLoginResult((String) response);
			if (result != null) {
				getAccount().saveUserinfo(result);
				
				
//				getProductList();
				getPermission();
			
			}
			break;
		case REQUEST_ID_PERMISSION:
			ResponseMessage responseMessage = ApiResponse.permission((String) response);
			if (responseMessage.status == 200){
				getAccount().savePermission();
				if (getAccount().isSeller()) {
					//卖家 用户中心
					GlobalUtil.startActivity(this, UserinfoActivity.class);
					setResult(Activity.RESULT_FIRST_USER);
				} else {
					//买家 返回
					setResult(Activity.RESULT_OK);
				}	
			}else {
				
			}
			finish();
			break;
		default:
			super.onCacheSuccess(requestId, requestInfo, response);
			break;
		}
	}

	@Override
	public void onCacheFailed(int requestId, RequestInfo requestInfo, CacheException error) {
		cancelProgressDialog();
		super.onCacheFailed(requestId, requestInfo, error);
		switch (requestId) {
		case REQUEST_ID_LOGIN:
			// 登录失败
			
			break;
		case REQUEST_ID_USERINFO:

			break;
		case REQUEST_ID_PERMISSION:
			//权限验证失败
			if (getAccount().isSeller()) {
				//卖家 停留登录页
			} else {
				//买家 go to 邀请码页面
				GlobalUtil.startActivity(this, CheckCodeActivity.class);
				finish();
			}	
			finish();
			break;
			
		default:
			
			break;
		}
	}

	private void drawerlistener() {
		mSlidingDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
			@Override
			public void onDrawerOpened() {
				mImageButton.setImageResource(R.drawable.z_arrow_down);
			}

		});

		mSlidingDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
			@Override
			public void onDrawerClosed() {
				mImageButton.setImageResource(R.drawable.z_arrow_up);
			}

		});

		mSlidingDrawer.setOnDrawerScrollListener(new SlidingDrawer.OnDrawerScrollListener() {
			@Override
			public void onScrollEnded() {
				//        mtext.setText("结束拖动");
			}

			@Override
			public void onScrollStarted() {
				//        mtext.setText("开始拖动");
			}

		});
	}
	
	@Override
	protected void onDestroy() {
		cancelProgressDialog();
	  super.onDestroy();
	}

	@Override
  public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_left_image_button:
			onBackPressed();
			break;

		default:
			break;
		}
	  
  }
	
	@Override
	public void finish() {
		cancelProgressDialog();
	  super.finish();
	}
}
